# -*- coding: utf-8 -*-

from resource_management import *
from resource_management.core.resources.system import Execute
from resource_management.core.source import *
from resource_management.libraries.functions.check_process_status import check_process_status
from resource_management.libraries.script.script import Script
from resource_management.core.resources.system import *
import commands
import os

def dolphin_env():
    print("海豚配置设置开始.....")
    import params
    Directory(params.dolphin_pidfile_dir,
              mode=0777,
              owner=params.dolphin_user,
              group=params.dolphin_group,
              create_parents=True
              )

    Directory(params.hdp_dolphin_log_dir,
              mode=0777,
              owner=params.dolphin_user,
              group=params.dolphin_group,
              create_parents=True
              )

    Directory(params.dolphin_log_dir,
              mode=0777,
              owner=params.dolphin_user,
              group=params.dolphin_group,
              create_parents=True)

    Directory(params.dolphin_conf_dir,
              mode=0777,
              owner=params.dolphin_user,
              group=params.dolphin_group,
              create_parents=True
              )

    # 数据目录？
    Directory(params.dolphin_common_map['data.basedir.path'],
              mode=0777,
              owner=params.dolphin_user,
              group=params.dolphin_group,
              create_parents=True
              )

    File(format(params.dolphin_env_path),
         mode=0777,
         content=InlineTemplate(params.dolphin_env_content),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    # 守护进程脚本,改了里面的一些路径配置,前提要做好java的软链
    File(format(params.dolphin_bin_dir + "/dolphinscheduler-daemon.sh"),
         mode=0775,
         content=Template("dolphin-daemon.sh.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    #checked
    File(format(params.dolphin_conf_dir + "/worker.properties"),
         mode=0755,
         content=Template("worker.properties.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    # checked
    File(format(params.dolphin_conf_dir + "/application-api.properties"),
         mode=0755,
         content=Template("application-api.properties.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    # checked
    File(format(params.dolphin_conf_dir + "/common.properties"),
         mode=0755,
         content=Template("common.properties.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    # checked
    File(format(params.dolphin_conf_dir + "/quartz.properties"),
         mode=0755,
         content=Template("quartz.properties.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    # checked
    File(format(params.dolphin_conf_dir + "/zookeeper.properties"),
         mode=0755,
         content=Template("zookeeper.properties.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )


    File(format(params.dolphin_conf_dir + "/alert.properties"),
         mode=0755,
         content=Template("alert.properties.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    File(format(params.dolphin_conf_dir + "/datasource.properties"),
         mode=0755,
         content=Template("datasource.properties.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    File(format(params.dolphin_conf_dir + "/logback-master.xml"),
         mode=0755,
         content=Template("master_logback.xml.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    File(format(params.dolphin_conf_dir + "/logback-alert.xml"),
         mode=0755,
         content=Template("alert_logback.xml.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    File(format(params.dolphin_conf_dir + "/logback-worker.xml"),
         mode=0755,
         content=Template("worker_logback.xml.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    File(format(params.dolphin_conf_dir + "/logback-api.xml"),
         mode=0755,
         content=Template("apiserver_logback.xml.j2"),
         owner=params.dolphin_user,
         group=params.dolphin_group
         )

    print("根据输入的java_home创建java软链.....")
    java_ln_check = os.path.exists("/usr/bin/java")
    if java_ln_check == False:
        os.system("ln -s {0} /usr/bin/java".format(params.java_path))
    else:
        pass






